<?php

namespace api\modules\v1\controllers\infusion;

use common\models\patient\DrugRecord;
use Yii;
use common\helpers\CommonHelper;
use services\infusion\TreatmentService;
use api\controllers\OnAuthController;
use common\models\patient\User;
use yii\web\HttpException;

/**
 * 排队
 * Class PatientController
 * @package api\modules\v1\controllers\infusion
 */
class TreatmentController extends OnAuthController
{
    /**
     * @var User
     */
    public $modelClass = User::class;

    /**
     * @api {get} /v1/infusion/treatment/patient-list 治疗-患者列表
     *
     */
    public function actionPatientList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $service = new TreatmentService();
        // 查询结果
        return $service->getPatientList($params, $page, $pageSize);
    }

    /**
     * @return array 药品操作类型【1新增、2治疗、3退费】
     */
    public function actionRecordType()
    {
        $list = [];
        foreach (DrugRecord::$type as $key => $val) {
            $list[$key - 1]['value'] = $key;
            $list[$key - 1]['title'] = $val;
        }
        return $list;
    }

    /**
     * @api {get} /v1/infusion/treatment/patient-drug-surplus 治疗-剩余数
     *
     */
    public function actionPatientDrugSurplus()
    {
        $params = Yii::$app->request->get();
        $patientId = $params['patient_id'];
        $service = new TreatmentService();
        // 查询结果
        return $service->patientDrugSurplus($patientId, 'not_select');
    }

    /**
     * @api {get} /v1/infusion/treatment/drug-surplus-select 治疗-筛选药品
     */
    public function actionDrugSurplusSelect()
    {
        $params = Yii::$app->request->get();
        $patientId = $params['patient_id'];
        $service = new TreatmentService();
        // 查询结果
        return $service->patientDrugSurplus($patientId);
    }

    /**
     * @api {get} /v1/infusion/treatment/add-patient-drug-surplus 治疗-新增患者药品剩余数
     */
    public function actionAddPatientDrugSurplus()
    {
        $params = Yii::$app->request->post();
        $service = new TreatmentService();
        // 查询结果
        return $service->updatePatientDrugSurplus($params, 'add');
    }

    /**
     * @api {get} /v1/infusion/treatment/refund-patient-drug-surplus 治疗-退费【患者药品剩余数】
     */
    public function actionRefundPatientDrugSurplus()
    {
        $params = Yii::$app->request->post();
        $service = new TreatmentService();
        // 查询结果
        return $service->updatePatientDrugSurplus($params, 'refund');
    }

    /**
     * @api {get} /v1/infusion/treatment/delete-patient-drug-surplus 治疗-删除患者药品
     */
    public function actionDeletePatientDrugSurplus()
    {
        $params = Yii::$app->request->post();
        $service = new TreatmentService();
        // 查询结果
        return $service->deletePatientDrugSurplus($params);
    }

    /**
     * @api {get} /v1/infusion/treatment/drug-record-list 患者输液记录
     */
    public function actionDrugRecordList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $service = new TreatmentService();
        // 查询结果
        return $service->drugRecordList($params, $page, $pageSize);
    }

    /**
     * @api {get} /v1/infusion/treatment/create-patient-drug-surplus 新增药品
     */
    public function actionCreatePatientDrugSurplus()
    {
        $params = Yii::$app->request->post();
        $service = new TreatmentService();
        // 查询结果
        return $service->createPatientDrugSurplus($params);
    }

    /**
     * @api {get} /v1/infusion/treatment/get-drug-list 药品列表
     */
    public function actionGetDrugList()
    {
        $service = new TreatmentService();
        // 查询结果
        return $service->getDrugList();
    }

    /**
     * @api {get} /v1/infusion/treatment/update-print-times 更新打印次数
     */
    public function actionUpdatePrintTimes()
    {
        $params = Yii::$app->request->post();
        $params['queue_id'] = $params['queue_id'] ?? 0;
        $params['print_type'] = $params['print_type'] ?? '';
        if (!$params['queue_id']) {
            return CommonHelper::api(422, '请选择要打印的排队药品');
        }
        if (!$params['print_type']) {
            return CommonHelper::api(422, '请选择要打印的类型');
        }
        $service = new TreatmentService();
        // 查询结果
        return $service->updatePrintTimes($params);
    }
}
